<?php
require '../../lib/init.php';
$gCn=Con();

$filename = 'kartinka.jpg';

$thumb = preg_replace('/(\.[a-zA-Z]{3,4})/', '_t${1}', $filename);


if($_POST) {
    $name=pg_escape_string($_POST['name']);
    $type_id=$_POST['type_id'];
    $category_id=$_POST['category_id'];
    $thumb_src=pg_escape_string($_POST['thumb_src']);
    $client_id=$_POST['client_id'];
    $year=pg_escape_string($_POST['year']);
    $show_front=$_POST['show_front'];
    $descr= $_POST['descr'];
    $video_src= pg_escape_string($_POST['video_src']);
    $orderby= (int)$_POST['orderby'];
    $opType=$_POST['optype'];
    $objID=$_POST['objid'];

    if($show_front==1) $show_front='true'; else $show_front='false';

    if($opType=='insert') {
        $sql='insert into objects(name,type_id,category_id,thumb_src,client_id,year,show_front,descr,orderby,video_src) values(\''.$name.'\','.$type_id.','.$category_id.',\''.$thumb_src.'\','.$client_id.',\''.$year.'\',\''.$show_front.'\',\''.$descr.'\','.$orderby.',\''.$video_src.'\')';
        $gCn->Execute($sql);
        $gCn->Execute('select object_id from objects order by object_id desc limit 1');
        $gCn->MoveFirst();
        $objID=(int)$gCn->mRs['object_id'];
    } else {
        $sql='update objects set
				name=\''.$name.'\',
				type_id='.$type_id.',
				category_id='.$category_id.',
				thumb_src=\''.$thumb_src.'\',
				client_id='.$client_id.',
				year=\''.$year.'\',
				video_src=\''.$video_src.'\',
				show_front='.$show_front.',
				orderby='.$orderby.',			
				descr=\''.$descr.'\'			
                                where object_id='.$objID.';';
        $gCn->Execute($sql);
    }

    header('Location: viewObjects.php');
    exit();
}
$opType='insert';

if($_GET['objid'] || $objID) {
    if(!$objID) $objID=$_GET['objid'];
    $gCn->Execute('select * from objects where object_id='.$objID.' limit 1');
    $gCn->MoveFirst();
    $name=$gCn->mRs['name'];
    $type_id=$gCn->mRs['type_id'];
    $category_id=$gCn->mRs['category_id'];
    $thumb_src=$gCn->mRs['thumb_src'];
    $client_id=$gCn->mRs['client_id'];
    $year=$gCn->mRs['year'];
    $video_src=$gCn->mRs['video_src'];
    $orderby= (int)$gCn->mRs['orderby'];
    if($gCn->mRs['show_front']=='true') $show_front=1; else $show_front=2;
    $descr=$gCn->mRs['descr'];
    $opType='update';

}

$sql='select * from clients;';
$gCn->Execute($sql);
$gCn->MoveFirst();
$clients_options='';
while(!$gCn->EOF()) {
    $clients_options.='<option value="'.$gCn->mRs['client_id'].'"';
    if($client_id==$gCn->mRs['client_id']) $clients_options.=' selected';
    $clients_options.='>'.$gCn->mRs['name'].'</option>';
    $gCn->MoveNext();
}
$gCn->Close();

?>


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert Object</title>
    </head>
    <body align="center">
        <div style="margin:0 auto; width:1000px;border:0px solid #a7a7a7;text-align:center;margin-top:100px;" align="center">
            <form name="form1" method="post" action="insertObject.php" >
                <h1>Insert Object</h1>
                <table border="1" align="center" cellpadding="5" width="700px">
                    <tr>
                        <td align="right">Име: </td>
                        <td align="left"><input name="name" type="text" value="<?=$name?>"></input></td>
                    </tr>
                    <tr>
                        <td align="right">Тип: </td>
                        <td align="left">
                            <select name="type_id">
                                <?php
                                // $objects['type_id'] е дефиниран в /lib/settings.php
                                foreach ($objects['type_id'] as $key=>$val) {
                                    ?>
                                <option value="<?=$key ?>" <?=($type_id==$key)?'selected':''?>><?=$val ?></option>';
                                    <?php
                                }
                                ?>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">Картинка src: </td>
                        <td align="left"><input name="thumb_src" size="40" type="text" value="<?=$thumb_src?$thumb_src:'Само името на голямата картинка'?>" /><br /> <sub>Голяма:500x300 Малка:240x144<br />(голяма.jpg->голяма_t.jpg)</sub></td>
                    </tr>
                    <tr>
                        <td align="right">Клиент: </td>
                        <td align="left">
                            <select name="client_id">
                                <option value="-1"></option>
                                <?=$clients_options?>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">Година: </td>
                        <td align="left"><input name="year" type="text" value="<?=$year?>"></input></td>
                    </tr>
                    <tr>
                        <td align="right">Категория: </td>
                        <td align="left">
                            <select name="category_id">
                                <option value="-1"></option>
                                <option value="1"<?=$category_id==1?'selected':''?>>ТВ</option>
                                <option value="2"<?=$category_id==2?'selected':''?>>Външна реклама</option>
                                <option value="3"<?=$category_id==3?'selected':''?>>Радио реклама</option>
                                <option value="4"<?=$category_id==4?'selected':''?>>web</option>
                                <option value="5"<?=$category_id==5?'selected':''?>>Вестници</option>
                                <option value="6"<?=$category_id==6?'selected':''?>>Брандиране</option>
                            </select>
                        </td>
                    </tr>

                    <tr>
                        <td align="right">Покажи на първа страница: </td>
                        <td align="left">
                            <select name="show_front">
                                <option value="1"<?=$show_front==1?'selected':''?>>Да</option>
                                <option value="2"<?=$show_front==2?'selected':''?>>Не</option>
                            </select>
                        </td>
                    </tr>
		    <tr>
                        <td align="right">SRC: </td>
                        <td align="left"><input name="video_src" type="text" value="<?=$video_src?>"></input></td>
                    </tr>
		    <tr>
                        <td align="right">Order by: </td>
                        <td align="left"><input name="orderby" type="text" value="<?=$orderby?>"></input></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
					Описание: <br>
                            <textarea name="descr" cols="80" rows="8"><?=$descr?></textarea>

                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
                            <input type="submit" value="Save"></input>

                        </td>
                    </tr>
                </table>
                <input type="hidden" name="optype" value="<?=$opType?>" />
                <input type="hidden" name="objid" value="<?=$objID?>" />
            </form>


        </div>


    </body>
</html>